package com.itheima.reggie.controller;/**
 * 功能描述
 *
 * @author 唯硕
 * @date 2022/09/16  16:33
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMessage;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 *@ClassName Controller
 *@Description TODO 表现层
 *@Author 唯硕
 *@Date 2022/9/16 16:33%
 *@Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> Login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());//密码进行MD5加密
//    定义查询条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee employee1 = employeeService.getOne(queryWrapper);
        if (employee1 == null) {
            R.error("登陆失败");
        }
        if (employee1.getPassword() != password) {
            R.error("登陆失败");
        }
        if (employee1.getStatus() == 0) {
            R.error("账号已禁用");
        }
//      登陆成功，将员工id存入Session并返回登陆成功结果
        request.getSession().setAttribute("employee", employee1.getId());

        return R.success(employee1);

    }

    @PostMapping("/logout")
    public R<String> Logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出登录");

    }

    //分页查询
    @GetMapping("/page")
    public R<Page> GetPage(int page, int pageSize, String name) {
        //构造分页构造器
        Page page1 = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(page1, queryWrapper);
        return R.success(page1);
    }

    //新增员工
    @PostMapping
    public R<String> AddEmployee(HttpServletRequest request, @RequestBody Employee employee) {
        //设置初始密码，MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

//获得当前登录用户的Id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);


        boolean save = employeeService.save(employee);
        return R.success("用户添加成功");
    }


    //根据Id修改员工信息
    @PutMapping
    public R<String> Modify(HttpServletRequest request,@RequestBody Employee employee) {
        log.info("传入数据为{}", employee.toString());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        Long emplId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(emplId);
        if (employeeService.updateById(employee))
        { long id = Thread.currentThread().getId();
            log.info("线程id为{}",id);
            return R.success("更新成功");}

        return R.error("更新失败");
    }

    @GetMapping("{id}")
    public  R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee!=null)
           return R.success(employee);
        return R.error("查询失败");
    }

}
